import random
import time
from common.base import BaseMethod


class AddGoods(BaseMethod):
    #打开首页
    def open(self,url):
        self.driver.get(url)

    # 进入商品添加页面
    def go_to_add_goodpage(self):
        self.click("xpath","//li[@class='first']/a")
        self.click("xpath","//span[@class='addition']")

    # 随机输入商品名称
    def input_rand_商品名称(self):
        good_name = "手机" + str(random.randint(100, 100000))
        # self.driver.find_element_by_id("goods_name").send_keys(good_name)
        self.inputKeys("id","goods_name",good_name)
    #指定输入商品名称
    def input_商品名称(self,name):
        self.inputKeys("id", "goods_name", name)
        # elem = self.driver.find_element_by_id("goods_name")
        # elem.clear()
        # elem.send_keys(name)

    # 输入商品货号
    def input_商品货号(self):
        item_num = "phone" + str(random.randint(100, 100000))
        # self.driver.find_element_by_id("goods_no").send_keys(item_num)
        self.inputKeys("id","goods_no",item_num)

    # 点击商品分类  ---- 手机
    def select_商品分类(self):
        self.click("xpath","//ul[@class='select']/li[32]")
        # self.driver.find_element_by_xpath("//ul[@class='select']/li[32]").click()

    # 商品推荐类型  --- 随机选择
    def select_商品推荐类型(self):
        elems = self.get_elements("name","goods_commend[]")
        random.choice(elems).click()

    # 点击发布商品
    def click_发布商品(self):
        self.click("xpath","//span[text()='发布商品']")

    #按照要求输入正确的商品数据
    def input_全部商品信息(self):
        self.input_rand_商品名称()
        self.input_商品货号()
        self.select_商品分类()
        self.select_商品推荐类型()

    # 通过url断言
    def assert_by_url(self, *value):
        hope = value[-1]
        time.sleep(3)
        self.assert_in(hope, self.get_url())

    # 通过标题断言
    def assert_by_tile(self, *value):
        hope = value[-1]
        time.sleep(3)
        self.assert_in(hope, self.get_title())

     # 通过元素的文本内容断言
    def assert_by_elem_text(self, *value):
        by, locate, hope = value
        time.sleep(2)
        actual = self.get_elem_text(by, locate)
        self.assert_in(hope, actual)
